package com.weds.edwx.web;

import com.weds.core.annotation.Logs;
import com.weds.core.base.BaseCommPager;
import com.weds.core.base.BaseController;
import com.weds.core.base.BasePageSearch;
import com.weds.core.resp.JsonResult;
import com.weds.core.utils.ImageUtils;
import com.weds.core.utils.StringUtils;
import com.weds.edpf.core.constant.ScmParams;
import com.weds.edwx.entity.*;
import com.weds.edwx.service.HairCutService;
import com.weds.edwx.service.HealthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @Author sjm
 * @Description 健康专栏
 * @Date 2020-02-28
 */
@RestController
@RequestMapping(value = "/health")
@Api(value = "健康专栏", description = "健康专栏")
public class HealthController extends BaseController {
    @Autowired
    private HealthService healthService;

    @Resource
    private ScmParams scmParams;

    private Logger log = LogManager.getLogger();


    @Logs
    @ApiOperation(value = "获取标题信息", notes = "获取标题信息")
    @RequestMapping(value = "/getTitleInfo", method = RequestMethod.POST)
    public JsonResult<List<HealthTitleInfoRecordEntity>> getTitleInfo(@RequestBody HealthTitleReq record) {
        List<HealthTitleInfoRecordEntity> list = healthService.getTitleInfo(record.getUserSerial());
        for (HealthTitleInfoRecordEntity entity : list) {
            if (!StringUtils.isBlank(entity.getImg())) {
                try {
                    if (!StringUtils.isBlank(scmParams.getScmUrl())) {
                        entity.setImg(scmParams.getScmUrl() + entity.getImg().replace("..", ""));
                    } else {
                        entity.setImg(ImageUtils.imgToBase64(scmParams.getRootPath() + File.separator  + "wwwroot" + entity.getImg().replace("..", "")));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return succMsgData((list));
    }

    // 总览页获取列表
    @Logs
    @ApiOperation(value = "总览页获取列表", notes = "总览页获取列表")
    @RequestMapping(value = "/getMainList", method = RequestMethod.POST)
    public JsonResult<BaseCommPager<HealthMainInfoRecordEntity>> getMainList(@RequestBody BasePageSearch<HealthMainReq> record) {
        setPageHelper(record);
        HealthMainReq entity = record.getSearch();
        if (entity == null) {
            entity = new HealthMainReq();
        }
        List<HealthMainInfoRecordEntity> list = healthService.getMainList(entity.getUserSerial());
        return succMsgData(new BaseCommPager<HealthMainInfoRecordEntity>(list));
    }

    // 获取消息详情
    @Logs
    @ApiOperation(value = "获取消息详情", notes = "获取消息详情")
    @RequestMapping(value = "/getMsgInfo", method = RequestMethod.POST)
    public JsonResult<HealthMsgInfoRecordEntity> getMsgInfo(@RequestBody HealthMsgReq record) {
        HealthMsgInfoRecordEntity entity = healthService.getMsgInfo(record);
            if (!StringUtils.isBlank(entity.getImgTop())) {
                try {
                    if (!StringUtils.isBlank(scmParams.getScmUrl())) {
                        entity.setImgTop(scmParams.getScmUrl() + entity.getImgTop().replace("..", ""));
                    } else {
                        entity.setImgTop(ImageUtils.imgToBase64(scmParams.getRootPath() + File.separator + "wwwroot" + entity.getImgTop().replace("..", "")));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (!StringUtils.isBlank(entity.getImgBottom())) {
                try {
                    if (!StringUtils.isBlank(scmParams.getScmUrl())) {
                        entity.setImgBottom(scmParams.getScmUrl() + entity.getImgBottom().replace("..", ""));
                    } else {
                        entity.setImgBottom(ImageUtils.imgToBase64(scmParams.getRootPath() + File.separator + "wwwroot" + entity.getImgBottom().replace("..", "")));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        return succMsgData(entity);
    }
}